package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.RefundPurchaseMapper;
import com.ruoyi.crm.domain.RefundPurchase;
import com.ruoyi.crm.service.IRefundPurchaseService;

/**
 * 采购退款单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-31
 */
@Service
public class RefundPurchaseServiceImpl implements IRefundPurchaseService
{
    @Autowired
    private RefundPurchaseMapper refundPurchaseMapper;

    /**
     * 查询采购退款单
     *
     * @param id 采购退款单主键
     * @return 采购退款单
     */
    @Override
    public RefundPurchase selectRefundPurchaseById(String id)
    {
        return refundPurchaseMapper.selectRefundPurchaseById(id);
    }

    /**
     * 查询采购退款单列表
     *
     * @param refundPurchase 采购退款单
     * @return 采购退款单
     */
    @Override
    public List<RefundPurchase> selectRefundPurchaseList(RefundPurchase refundPurchase)
    {
        return refundPurchaseMapper.selectRefundPurchaseList(refundPurchase);
    }

    /**
     * 新增采购退款单
     *
     * @param refundPurchase 采购退款单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRefundPurchase(RefundPurchase refundPurchase)
    {
        int rows = refundPurchaseMapper.insertRefundPurchase(refundPurchase);
        insertCustomerProduct(refundPurchase);
        return rows;
    }

    /**
     * 修改采购退款单
     *
     * @param refundPurchase 采购退款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRefundPurchase(RefundPurchase refundPurchase)
    {
        refundPurchase.setUpdateTime(DateUtils.getNowDate());
        refundPurchaseMapper.deleteCustomerProductByCustomerName(refundPurchase.getId());
        insertCustomerProduct(refundPurchase);
        return refundPurchaseMapper.updateRefundPurchase(refundPurchase);
    }

    /**
     * 批量删除采购退款单
     *
     * @param ids 需要删除的采购退款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRefundPurchaseByIds(String[] ids)
    {
        return refundPurchaseMapper.deleteRefundPurchaseByIds(ids);
    }

    /**
     * 删除采购退款单信息
     *
     * @param id 采购退款单主键
     * @return 结果
     */
    @Override
    public int deleteRefundPurchaseById(String id)
    {
        refundPurchaseMapper.deleteCustomerProductByCustomerName(id);
        return refundPurchaseMapper.deleteRefundPurchaseById(id);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param refundPurchase 采购退款单对象
     */
    public void insertCustomerProduct(RefundPurchase refundPurchase)
    {
        List<CustomerProduct> customerProductList = refundPurchase.getCustomerProductList();
        String id = refundPurchase.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                refundPurchaseMapper.batchCustomerProduct(list);
            }
        }
    }
}